//
//  PopMenu.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/11.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "PopMenu.h"

#import "LXMallFrame.h"

#import "LXOptionButton.h"

#import "LXScanerController.h"

#import "LXMallShareController.h"
@interface PopMenu(){
    CGFloat _endY;
    CGPoint _orginPoint;
    NSTimeInterval _durition;
}

@property(nonatomic,strong)UIWindow * window;

@property(nonatomic,strong)UIButton * coverView;

@property(nonatomic,assign)BOOL isPop;

@property(nonatomic,strong)UIImageView * imgv;

@property(nonatomic,strong)UIButton * myMessege;

@property(nonatomic,strong)UIButton * scanner;

@property(nonatomic,strong)UIButton * myQrCode;

@property(nonatomic,strong)UIButton * useHelp;

@property(nonatomic,strong)NSMutableArray * points;
@property(nonatomic,strong)NSMutableArray * buttons;
@end


@implementation PopMenu
+(instancetype)popMenu{
    return [[[self class] alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
}
-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        
        NSArray * titles = @[@"消息",@"扫一扫",@"我的二维码",@"使用帮助"];
        NSArray * images = @[@"xiaoxi",@"saoyisao",@"erweima",@"bangzhu"];
        for (int i = 0; i<4; i++) {
            UIButton * option = [self buttonWithTitle:titles[i] image:[UIImage imageNamed:images[i]]  tag:i];
            [self addSubview:option];
        }
        _durition = 0.12;
        [self addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}



-(void)hidden{
    __block UIButton * btn = (UIButton*)[self viewWithTag:-222];

    __block UIButton * btn1 = (UIButton*)[self viewWithTag:0+10];
    __block UIButton * btn2 = (UIButton*)[self viewWithTag:1+10];
    __block UIButton * btn3 = (UIButton*)[self viewWithTag:2+10];
    __block UIButton * btn4 = (UIButton*)[self viewWithTag:3+10];
    [UIView animateWithDuration:_durition*4 animations:^{
        btn.transform = CGAffineTransformIdentity;
    }];
    [UIView animateWithDuration:_durition animations:^{
        btn4.centerY = ScreenHeight - 20;
        btn4.x = (ScreenWidth - btn4.width)*0.5;
        
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:_durition animations:^{
            btn3.centerY = ScreenHeight - 20;
            btn3.x = (ScreenWidth - btn3.width)*0.5;
            btn4.hidden = YES;
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:_durition animations:^{
                btn2.centerY = ScreenHeight - 20;
                btn2.x = (ScreenWidth - btn2.width)*0.5;
                btn3.hidden = YES;

            }completion:^(BOOL finished) {
                [UIView animateWithDuration:_durition animations:^{
                    btn1.centerY = ScreenHeight - 20;
                    btn1.x = (ScreenWidth - btn1.width)*0.5;
                    btn2.hidden = YES;

                }completion:^(BOOL finished) {
                    btn1.hidden = YES;

                    if(self.hiddenBlock){
                        self.hiddenBlock();
                    }
                }];
                
            }];
        }];
    }];
    
}

-(void)hidden:(void (^)())complete{
    [self hidden];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_durition*4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if(complete){
            complete();
        }
    });
}

-(void)show{
    [self.window addSubview:self];
    __block UIButton * btn = (UIButton*)[self viewWithTag:-222];
    __block UIButton * btn1 = (UIButton*)[self viewWithTag:0+10];
    __block UIButton * btn2 = (UIButton*)[self viewWithTag:1+10];
    __block UIButton * btn3 = (UIButton*)[self viewWithTag:2+10];
    __block UIButton * btn4 = (UIButton*)[self viewWithTag:3+10];
    
    [UIView animateWithDuration:_durition*4 animations:^{
        btn.transform = CGAffineTransformRotate(btn.transform, M_PI*0.25);
    }];

    [UIView animateWithDuration:_durition animations:^{
        btn1.y = _endY;
        btn1.x = (btn1.tag-10)*(15+btn1.width)+15;
        btn1.hidden = NO;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:_durition animations:^{
            btn2.y = _endY;
            btn2.x = (btn2.tag-10)*(15+btn1.width)+15;
            btn2.hidden = NO;
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:_durition animations:^{
                btn3.y = _endY;
                btn3.x = (btn3.tag-10)*(15+btn1.width)+15;
                btn3.hidden = NO;
            }completion:^(BOOL finished) {
                [UIView animateWithDuration:_durition animations:^{
                    btn4.y = _endY;
                    btn4.x = (btn4.tag-10)*(15+btn1.width)+15;
                    btn4.hidden = NO;
                }];

            }];
        }];
    }];
    
}
-(void)OptionButtonClick:(UIButton*)btn{
    
    [self hidden:^{
        switch (btn.tag-10) {
            case 0:{
                
            }break;
            case 1:{
                LXScanerController * scanVC = [[LXScanerController alloc] init];
                [[self getCurrentVC].navigationController  pushViewController:scanVC animated:YES];
            }break;
            case 2:{
                LXMallShareController * shareVC = [[LXMallShareController alloc] init];
                [[self getCurrentVC].navigationController pushViewController:shareVC animated:YES];
            }break;
            case 3:{
                
            }break;
                
            default:
                break;
        }
    }];
    
}

-(UIButton*)buttonWithTitle:(NSString*)title image:(UIImage*)image tag:(NSInteger)tag{


    CGFloat space = 15;
    CGFloat width = (ScreenWidth - 5*space)/4;
    CGFloat height = width * 1.2;
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake((ScreenWidth-width)*0.5, 0, width, height);
    btn.centerY = ScreenHeight - 20;
    btn.hidden = YES;
    _endY = ScreenHeight-btn.height - 125;
    btn.titleLabel.font = [UIFont systemFontOfSize:ScreenWidth>320?14:10];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.tag = tag+10;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(OptionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.imageEdgeInsets = UIEdgeInsetsMake(-10, (btn.width-btn.currentImage.size.width)*0.5, btn.height-btn.width, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(width, -btn.width+12, 0, 0);
    return btn;
}
-(UIWindow*)window{
    if(_window == nil){
        _window = [[UIApplication sharedApplication].delegate window];
    }
    return _window;
}

-(UIButton*)coverView{
    if(_coverView == nil){
        _coverView = [[UIButton alloc] initWithFrame:self.window.bounds];
        _coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        [_coverView addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
    }
    return _coverView;
}

@end
